import requests
from pyquery import PyQuery as pq
import re
import json
import os
import shutil

# 个人照：https://www.saxstar.club/pic/activity.html
# 写真：https://www.saxstar.club/pic/activity2.html
# blog：https://www.saxstar.club/pic/activity4.html
# 杂志：https://www.saxstar.club/pic/hiding_nogi.html
# 微博图库：https://www.saxstar.club/pic/xinlang_copy.html
# url: https://www.saxstar.club/nogizaka/getImageUrl?name=feiniao_yoda.txt

# 注意：开始前需修改存储文件位置的文件夹名称！！！

# 微博图库
# URL = 'https://www.saxstar.club/pic/xinlang_copy.html'
# 杂志
# URL = 'https://www.saxstar.club/pic/hiding_nogi.html'
# blog
# URL = 'https://www.saxstar.club/pic/activity4.html'
# 写真
# URL = 'https://www.saxstar.club/pic/activity2.html'
# 个人照
URL = 'https://www.saxstar.club/pic/activity.html'

headers = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
        }

class Nogizaka():

    def __init__(self):

        self.url = URL
        self.headers = headers


    # 请求页面
    def get_request(self):
        
        response = requests.get(url=self.url, headers=self.headers).text
        return response


    # 获取api txt列表的节点
    def analysis(self, html):
        
        doc = pq(html)
        node_list = doc('.member')
        text_list = []
        for item in node_list.items():
            text_list.append((item.attr('id')))
        # print(text_list)
        return text_list


    # 拼接获得完整txt列表
    def split_url(self, *name):

        base_url = 'https://www.saxstar.club/nogizaka/getImageUrl?name='
        url_api = []
        for name_list in name:
            for item in name_list:
                url_api.append(base_url + item + '.txt')
                # 下载个人照时用此方法拼接
                # url_api.append(base_url + item + '_dou.txt')
        return url_api


    # 获取图片原始地址的URL字典列表
    def visit_real_api(self, *img_txt):

        dict_url = []
        url_list = []
        # print(img_txt)
        for item in img_txt:
            for txt in item:
                dict_url = requests.get(url=txt, headers=self.headers).json()
                url_list.append(dict_url[0])
            
        return url_list


    # 存储图片
    def storage_img(self, folder, *img_dict):
        # print(img_dict)
        for item in img_dict:
            # print(item)
            for img in item:
                # 判断非空
                if img:
                    # print(img)
                    # print(type(img))
                    filename = img.split('/')[-1]
                    # print(filename)

                    # 拼接图片地址以获取最高清图片
                    new_img = list(img)
                    # print(new_img[37])
                    new_img[37] = 'r'
                    img_row = ''.join(new_img)
                    # print(img_row)

                    # 开始请求图片地址并存储到本地
                    res = requests.get(img_row, headers=headers)
                    try:
                        with open('/Users/mac/Documents/nogizaka/personal/' + folder + '/' + filename, 'wb') as f_obj:
                            f_obj.write(res.content)
                    except Exception as e:
                        print(e)
                    else:
                        print('下载成功！' + filename)
                    # break



    def main(self):
        
        html = self.get_request()
        name = self.analysis(html)
        # print(name)
        # print(type(name))
        # print(len(name))
        

        # 从断点处开始
        name = name[1:]

        txt = self.split_url(name)
        # print(txt[0])
    
        image_api_list = self.visit_real_api(txt)

        # print(len(image_api_list))
        # print(image_api_list[0])
        folder = ''
        for index, item in enumerate(image_api_list):
            # print(item)
            # print(index)
            # print(name[index])
            # break

            # 打印下载进度标识
            print('='*50)
            print('正在下载第%s个txt:%s' %(index + 1, name[index]))

            # 新建文件夹
            folder = name[index].split('.')[0]
            if not os.path.exists('/Users/mac/Documents/nogizaka/personal/' + folder):
                try:
                    os.makedirs('/Users/mac/Documents/nogizaka/personal/' + folder)
                except Exception as e:
                    print(e)
            
        
            for key, value in item.items():
                self.storage_img(folder, value)
            # break


if __name__ == "__main__":
    
    img = Nogizaka()
    img.main()
